package com.litong.litongjiaoyu.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.litong.litongjiaoyu.controller.WxPushServiceQcl;
import com.litong.litongjiaoyu.dao.HomeworkDao;
import com.litong.litongjiaoyu.entity.StuHomework;
import com.litong.litongjiaoyu.entity.WxMaTemplateData;
import com.litong.litongjiaoyu.entity.WxMssVo;
import com.litong.litongjiaoyu.service.CommenService;
import com.litong.litongjiaoyu.service.HomeworkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

/**
 * @ClassName HomeworkImpl
 * @Description 作业操作接口
 * @Author jingz
 * @Date 2019/8/1 9:41
 * @Version 1.0
 **/
@Service
public class HomeworkImpl implements HomeworkService {

    @Autowired
    private HomeworkDao homeworkDao;

    @Autowired
    private WxPushServiceQcl wxPushServiceQcl;
    @Autowired
    private CommenService commenService;

    private final String Template_id = "IQSQGOszEi80s4PhjokL-dhL46zTvSkrSOZQdzLg5NQ";
    private final String appid = "wx78763b2bea2646c2";
    private final String appSecret = "571a3836b6b557d2e7dc1aaeabdc0be2";

    /**
     * 获取今日作业数目
     * @param className
     * @param campus
     * @return
     */
    @Override
    public Integer getNumber(String className, String campus) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

        java.util.Date date = new java.util.Date();

        Date sqlDate = new Date(date.getTime());

        return homeworkDao.getNumber(className, sqlDate, campus);
    }

    /**
     * 添加作业
     *
     * @param homework
     * @return
     */
    @Transactional
    @Override
    public int insertHomework(StuHomework homework) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

        java.util.Date date = new java.util.Date();

        Date sqlDate = new Date(date.getTime());

        homework.setDate(sqlDate);


        Map<String, WxMaTemplateData> data = new HashMap<String, WxMaTemplateData>(3);

//      预约人
        WxMaTemplateData wxMaTemplateData1 = new WxMaTemplateData();
        wxMaTemplateData1.setValue(homework.getClassname());
        data.put("thing1", wxMaTemplateData1);

//      预约时间
        WxMaTemplateData wxMaTemplateData2 = new WxMaTemplateData();
        wxMaTemplateData2.setValue(simpleDateFormat.format(date));
        data.put("date2", wxMaTemplateData2);

//      预约课程
        WxMaTemplateData wxMaTemplateData3 = new WxMaTemplateData();
        wxMaTemplateData3.setValue("今日作业已发布，请进入小程序查看今日作业");
        data.put("thing3", wxMaTemplateData3);

//      设置接收人、模板id，appid，appSecret
        WxMssVo wxMssVo = new WxMssVo();

        List<String> strings = commenService.getOpenIdByClassAndCampus(homework.getCampus(), homework.getClassname());
        for (String string : strings) {
            wxMssVo.setTouser(string);
            wxMssVo.setPage("pages/homework/homework");
            wxMssVo.setData(data);
            wxMssVo.setTemplate_id(Template_id);
            //        获取消息发送结果
            JSONObject result = wxPushServiceQcl.pushOneUser( appid, appSecret, wxMssVo);
        }


        return homeworkDao.insertHomework(homework);
    }

    /**
     * 根据openid获取今日班级作业
     * @param className
     * @param campus
     * @return
     */
    @Override
    public List<StuHomework>  queryHomework(String className, String campus) {
        java.util.Date date = new java.util.Date();

        Date sqlDate = new Date(date.getTime());
        return homeworkDao.queryHomework(className, sqlDate, campus);
    }

    /**
     * 删除不是今日的作业内容
     * @return
     */
    @Transactional
    @Override
    public int delete() {
        java.util.Date date = new java.util.Date();

        Date sqlDate = new Date(date.getTime());
        return homeworkDao.delete(sqlDate);
    }
}
